//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.qzsoft.bootstrap.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.qzsoft.jeemis.platform.security.user.SecurityUser;
import com.qzsoft.jeemis.platform.security.user.UserDetail;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author gbx
 */
@Component
@Primary
public class FieldsMetaObjectHandler implements MetaObjectHandler {

    /***************
     * 2020-11-13 14:13:50 增加自动插入字段是否存在的检验 高丙祥 MOD END
     ***************/
    @Override
    public void insertFill(MetaObject metaObject) {
        Class<?> creator = null;
        Class<?> updater = null;
        Class<?> createDate = null;
        Class<?> updateDate = null;
        String[] setterNames = metaObject.getSetterNames();
        if (setterNames != null && setterNames.length > 0) {
            for (String item : setterNames) {
                if ("creator".equals(item)) {
                    creator = metaObject.getSetterType("creator");
                    continue;
                }
                if ("updater".equals(item)) {
                    updater = metaObject.getSetterType("updater");
                    continue;
                }
                if ("createDate".equals(item)) {
                    createDate = metaObject.getSetterType("createDate");
                    continue;
                }
                if ("updateDate".equals(item)) {
                    updateDate = metaObject.getSetterType("updateDate");
                    continue;
                }
            }
        }

        // 填充数据
        UserDetail user = SecurityUser.getUser();
        if(null != user) {
            if (creator != null && null != user.getId()) {
                if (creator.isInstance(new String())) {
                    this.setFieldValByName("creator", user.getId().toString(), metaObject);
                } else {
                    this.setFieldValByName("creator", user.getId(), metaObject);
                }
                this.setFieldValByName("creatorName", user.getRealName(), metaObject);
            }
            if (updater != null && null != user.getId()) {
                if (updater.isInstance(new String())) {
                    this.setFieldValByName("updater", user.getId().toString(), metaObject);
                } else {
                    this.setFieldValByName("updater", user.getId(), metaObject);
                }
                this.setFieldValByName("updateName", user.getRealName(), metaObject);
            }
            

            // 因为部门信息有前台传递添加的情况，所以这里判断是否为null
            if (metaObject.hasSetter("deptId") && null == metaObject.getValue("deptId") && null != user.getDeptId()) {
                this.setFieldValByName("deptId", user.getDeptId().toString(), metaObject);
            }

            // 因为部门信息有前台传递添加的情况，所以这里判断是否为null
            if (metaObject.hasSetter("deptPkid") && null == metaObject.getValue("deptPkid") && null != user.getDeptPkid()) {
                this.setFieldValByName("deptPkid", user.getDeptPkid().toString(), metaObject);
            }
        }
        
        // 修改时间、添加时间
        Date date = new Date();
        if (createDate != null) {
            this.setFieldValByName("createDate", date, metaObject);
        }
        if (updateDate != null) {
            this.setFieldValByName("updateDate", date, metaObject);
        }

        // 判断是否逻辑删除字段
        if (metaObject.hasSetter("deleted")) {
            setFieldValByName("deleted", "0", metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Class<?> updater = null;
        Class<?> updateDate = null;
        String[] setterNames = metaObject.getSetterNames();
        if (setterNames != null && setterNames.length > 0) {
            for (String item : setterNames) {
                if ("updater".equals(item)) {
                    updater = metaObject.getSetterType("updater");
                    break;
                }
            }
            for (String item : setterNames) {
                if ("updateDate".equals(item)) {
                    updateDate = metaObject.getSetterType("updateDate");
                    break;
                }
            }
        }

        if (updater != null) {
            if (updater.isInstance(new String())) {
                this.setFieldValByName("updater", SecurityUser.getUserId().toString(), metaObject);
            } else {
                this.setFieldValByName("updater", SecurityUser.getUserId(), metaObject);
            }
            this.setFieldValByName("updateName", SecurityUser.getUser().getRealName(), metaObject);
        }

        if (updateDate != null) {
            this.setFieldValByName("updateDate", new Date(), metaObject);
        }
    }
    /***************
     * 2020-11-13 14:13:50 增加自动插入字段是否存在的检验 高丙祥 MOD END
     ***************/
}
